#####################################################
# Replication Archive: Crumbling in the Face of Cost?
# Mader, Neubert, Münchow, Hofmann, Schoen & Gavras
# 01/2024
#####################################################
# Figure 2: Average treatment effects 
#####################################################

# define which packages are needed 
p_needed <-
  c("broom", "hrbrthemes", "viridis", "ggh4x", "tidyverse",
    "sjPlot", "dotwhisker", "gridExtra", "grid", "extrafont",
    "ggrepel", "hrbrthemes", "ggExtra")

# check which packages are already installed
packages <- rownames(installed.packages())

# check which of the packages you need are not installed 
p_to_install <- p_needed[!(p_needed %in% packages)]

# install missing packages 
if (length(p_to_install) > 0) {
  install.packages(p_to_install)
}
# load required packages 
sapply(p_needed, require, character.only = TRUE)

###################################################

# load data
load("EUP_Costs_ReplicationData.RData")


# Experiment 1: Pooled without controls
experiment1_Aggregate_p <- dat %>%
  mutate(costExperiment1_Treat1 = fct_recode(costExperiment1_Treat1, socialCosts = "SoldiersRisk")) %>%
  dplyr::select(costExperiment1_CrisisManagement, costExperiment1_Treat1, costExperiment1_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment1_Treat1, costExperiment_Treat2 = costExperiment1_Treat2) %>%
  do(broom::tidy(lm(costExperiment1_CrisisManagement ~  costExperiment_Treat1 + costExperiment_Treat2 + QCOUNTRY, data = .))) %>%
  dplyr::mutate(dv = "Military Intervention",
                model = "Without Controls") %>%
  dplyr::filter(str_detect(term, "costEx"))

# Experiment 2: Pooled without controls
experiment2_Aggregate_p <- dat %>%
  mutate(costExperiment2_Treat1 = fct_recode(costExperiment2_Treat1, socialCosts = "JobCuts")) %>%
  dplyr::select(costExperiment2_Aircraft, costExperiment2_Treat1, costExperiment2_Treat2, QCOUNTRY, female, education, age, attitudes_EU, attitudes_NATO, attachmentCountry, attachmentEurope, compromise, militarism, threat, quiz, governmentVote, csdp_wouldlike, leftright) %>%
  dplyr::rename(costExperiment_Treat1 = costExperiment2_Treat1, costExperiment_Treat2 = costExperiment2_Treat2) %>%
  do(broom::tidy(lm(costExperiment2_Aircraft ~  costExperiment_Treat1 + costExperiment_Treat2 + QCOUNTRY, data = .))) %>%
  mutate(dv = "Defence Procurement",
         model = "Without Controls") %>%
  dplyr::filter(str_detect(term, "costEx"))

# bind results of both experiments for visualization
both_experiments <- rbind(experiment1_Aggregate_p,
                          experiment2_Aggregate_p)

# Name results for visualization
both_experiments$term[1] <- "Non-Budgetary \nCosts: Casualties"
both_experiments$term[2] <- "Budgetary Costs: \nNational Budget"
both_experiments$term[3] <- "Budgetary Costs: \nPooled Budget"
both_experiments$term[4] <- "Non-Budgetary \nCosts: Job Cuts"
both_experiments$term[5] <- "Budgetary Costs: \nNational Budget"
both_experiments$term[6] <- "Budgetary Costs: \nPooled Budget"


# transform terms as factors and relevel them for visualization
both_experiments$term <- as.factor(both_experiments$term)
both_experiments$term <- forcats::fct_relevel(both_experiments$term,
                                              "Non-Budgetary \nCosts: Casualties",
                                              "Non-Budgetary \nCosts: Job Cuts",
                                              "Budgetary Costs: \nNational Budget",
                                              "Budgetary Costs: \nPooled Budget")
# reverse factors for visualization
both_experiments <- both_experiments %>%
  mutate(term = forcats::fct_rev(term))

# Plot: Pooled results from both experiments
plotExperiment_Aggregate <- ggplot(both_experiments,
                                   aes(estimate, fct_rev(term), color = model
                                   )) +
  geom_point(size = 3.5, position = position_dodge(.2)) +
  geom_linerange(aes(xmin = estimate -1.96*std.error,
                     xmax = estimate + 1.96*std.error),
                 size = 1.5,
                 position = position_dodge(.2),
                 alpha = 0.7) +
  theme_ipsum_rc(grid="Y",
                 axis_title_size = 18,
                 axis_text_size = 18,
                 strip_text_size = 24,
                 strip_text_face = "bold") +
  geom_vline(xintercept = 0, lty = 2, color = "darkgrey", size = 0.75) +
  labs(y = "", x = "") +
  facet_grid(~(factor(dv, levels = c("Military Intervention", "Defence Procurement"))), scales = "free") +
  theme(legend.position = "bottom",
        legend.title= element_blank()) +
  lims(x = c(-0.15,0.06)) +
  coord_flip() +
  scale_color_manual(values = c("black", "darkgrey")) +
  theme(text = element_text(size = 20),
        legend.position = "none",
        strip.text.x = element_text(hjust = 0.5))

# inspect plot
plotExperiment_Aggregate

# save plot
ggsave("plots/mainTreatmentEffect_Experiment_v2.png", plotExperiment_Aggregate, device = "jpeg", width = 4200 , height = 2000, units = "px")
